package com.maoshi.shop.platform.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.bean.enums.ReturnMoneyStsType;
import com.maoshi.shop.bean.model.OrderRefund;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.service.OrderRefundService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Component("orderRefundTask")
public class OrderRefundTask {

    @Autowired
    private OrderRefundService orderRefundService;



    /**
     * 取消申请超时的订单，无论该超时订单处于任何状态
     */
    public void cancelWhenTimeOut() {
        log.info("==============订单退款超时处理开始===================");
        // 设定时间值
        Date date = DateUtil.offsetDay(new Date(), -Constant.MAX_REFUND_APPLY_TIME);
        // 获取待处理的退款订单
        List<OrderRefund> orderRefundList = orderRefundService.list(new LambdaQueryWrapper<OrderRefund>()
                .in(OrderRefund::getReturnMoneySts, Arrays.asList(ReturnMoneyStsType.APPLY.value(),ReturnMoneyStsType.PROCESSING.value(),ReturnMoneyStsType.CONSIGNMENT.value(),ReturnMoneyStsType.RECEIVE.value()))
                .lt(OrderRefund::getApplyTime, date));
        if (CollectionUtils.isNotEmpty(orderRefundList)) {
            orderRefundService.cancelWhenTimeOut(orderRefundList);
        }
        log.info("==============订单退款超时处理结束===================");
    }

}
